<?php
namespace app\reduction\model;

use app\BaseModel;
use think\facade\Db;

class UserRecord extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_reduction_user_record';


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 11:53
     * @功能说明:查询数量
     */
    public function recordCount($dis){

        $count = $this->where($dis)->count();

        return is_numeric($count)?$count:0;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-22 10:13
     * @功能说明:能使用的数量
     */
    public function canUseCount($user_id){

        $dis = [
            'user_id'=>$user_id,

            'status' => 1
        ];

        $num = $this->recordCount($dis);

        return is_numeric($num)?$num:0;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-21 14:45
     * @功能说明:领取
     */
    public function addRecord($user_id,$staff_id,$active){

        $data = [

            'uniacid'      => $active['uniacid'],
            //用户id
            'user_id'      => $user_id,
            //员工id
            'staff_id'     => $staff_id,
            //活动id
            'active_id'    => $active['id'],
            //名称
            'coupon_name'  => $active['title'],
            //最低消费金额
            'full'         => $active['full'],
            //优惠金额
            'reduction'    => $active['reduction'],
            //返佣方式
            'selling_type' => $active['selling_type'],
            //佣金
            'cash'         => $active['cash'],
            //比列
            'balance'      => $active['balance'],
            //过期时间
            'over_time'    => $active['over_time']*86400+time(),
            //创建时间
            'create_time'  => time(),
            //状态
            'status'       => 1,
        ];

        $res = $this->insert($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 14:40
     * @功能说明:初始化一下
     */
    public function initRecord(){

        $dis[] = ['status','=',1];

        $dis[] = ['over_time','<',time()];

        $res   = $this->where($dis)->update(['status'=>3]);

        return $res;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-21 10:06
     * @功能说明:活动详情
     */
    public function recordInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():$data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 10:07
     * @功能说明:活动编辑
     */
    public function recordUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-21 15:02
     * @功能说明:列表
     */
    public function recordList($dis,$page){

        $data = $this->alias('a')
                ->join('longbing_card_user b','a.user_id = b.id','left')
                ->where($dis)
                ->field(['a.*','b.nickName','b.avatarUrl'])
                ->group('a.id')
                ->order('a.id desc')
                ->paginate($page)
                ->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //创建时间
                $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                //使用时间
                $v['use_time']    = date('Y-m-d H:i:s',$v['use_time']);
                //过期时间
                $over_time = $v['over_time'] - time();
                //处理成友好时间
                $v['over_time'] = lbGetDatesNoMonth($over_time);

                $v['over_time'] = $v['status']==3||$v['status']==2?'--':$v['over_time'];

                $v['use_time']  = $v['status']==3||$v['status']==1?'--':$v['use_time'];

                $v['pay_price'] = $v['status']==3||$v['status']==1?'--':$v['pay_price'];
                //用户头像
                $v['avatarUrl'] = !empty($v['avatarUrl'])?$v['avatarUrl']:'https://lbqnyv2.migugu.com/defaultAvatar.png';
                //员工姓名
                $v['staff_name']= lbUserName($v['staff_id']);
            }
        }
        return $data;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-04-21 15:02
     * @功能说明:前端用户满减列表
     */
    public function userRecordList($dis){

        $data = $this->where($dis)->order('id desc')->paginate(10)->toArray();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){
                //过期时间
                $over_time = $v['over_time'] - time();
                //处理成友好时间
                $v['over_time'] = lbGetDates($over_time);
                //员工头像
                $v['staff_img'] = lbUserAvatar($v['staff_id']);
                //员工姓名
                $v['staff_name']= lbUserName($v['staff_id']);
            }
        }
        return $data;
    }









}